;  This file is part of tornado.
; 
;    tornado is free software: you can redistribute it And/Or modify
;    it under the terms of the GNU General Public License As published by
;    the Free Software Foundation, either version 3 of the License, Or
;    any later version.
; 
;    tornado is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY Or FITNESS For A PARTICULAR PURPOSE.  See the
;    GNU General Public License For more details.
; 
;    You should have received a copy of the GNU General Public License
;    along With tornado.  If Not, see <http://www.gnu.org/licenses/>. 2
; 

IncludeFile "../../build/tornado.pbi"

#GUID = "CHAT"
#VERSION = "1.0.0"

Enumeration 1
  #CLIENT_SEND_SMS
  #SERVER_SEND_SMS
EndEnumeration

*server = createGameServer(#GUID,6500,"CHAT-ROOM",#VERSION,32)

OpenConsole()

If *server
  
While #True 
  gameServerRun(*server)  
  
  
  ;
  ; receive message
  ;
  If serverPacketQueued(*server) = #True 
    *receivePacket = serverGetReceivedPacket(*server)
    header.a       = #Null 
    
    popPacket(*receivePacket,@header)
    
    If header = #CLIENT_SEND_SMS

      from.s    = Space(512)
      message.s = Space(2048)
      popPacket(*receivePacket,@from)
      popPacket(*receivePacket,@message)
      
      
      *sendPacket = createPacket()
      
      sHeader.a = #SERVER_SEND_SMS
      pushPacket(*sendPacket,@sHeader,#TYPE_ASCII)
      pushPacket(*sendPacket,@from,#TYPE_STRING)
      pushPacket(*sendPacket,@message,#TYPE_STRING)
      
      serverSetPacketDestination(*server, *sendPacket, -1)
      serverAddPacket(*sendPacket, *server)
      
      PrintN("Receive message from "+from+" : " + message)
      
    EndIf 
    
    
    freePacket(*receivePacket)
  EndIf 
  
  
  
Wend 

Delay(10)
EndIf 
